<?php

namespace App\Http\Controllers\Personal;

use App\Facades\Site;
use App\Models\Common\CollTown;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Common\CollTwoOption;
use App\Models\Personal\ResumeCertificate;
use App\Models\Personal\ResumeEducationExp;
use App\Models\Personal\ResumeLanguageSkill;
use App\Models\Personal\ResumeOther;
use App\Models\Personal\ResumeProjectExp;
use App\Models\Personal\ResumeSkill;
use App\Models\Personal\ResumeWorkExp;
use App\Models\Personal\ResumeWorks;
use App\Models\Personal\ResumeWorksImg;
use Illuminate\Http\Request;
use App\Models\Personal\Resume;
use App\Http\Requests\Personal\MyResume\MyResumeRequest;
use App\Http\Requests\Personal\ResumeMange\resumeBasicRequest;
use App\Http\Requests\Personal\ResumeMange\resumeObjectiveRequest;
use App\Http\Requests\Personal\ResumeMange\addWorkExpRequest;
use App\Http\Requests\Personal\ResumeMange\modWorkExpRequest;
use App\Http\Requests\Personal\ResumeMange\addEducationExpRequest;
use App\Http\Requests\Personal\ResumeMange\modEducationExpRequest;
use App\Http\Requests\Personal\ResumeMange\modProjectExpRequest;
use App\Http\Requests\Personal\ResumeMange\addProjectExpRequest;
use App\Http\Requests\Personal\ResumeMange\addOtherRequest;
use App\Http\Requests\Personal\ResumeMange\modOtherRequest;
use App\Http\Requests\Personal\ResumeMange\addWorksRequest;
use App\Http\Requests\Personal\ResumeMange\modWorksRequest;
use App\Http\Requests\Personal\resumeRefresh\resumeRefreshRequest;
use Style, DB;

/**
 * @param //个人简历控制器
 * 创建时间 2016-8-13
 **/
class ResumeController extends CommonController{
    private $user;

    public function __construct(){
        $this->user = auth()->guard('personal')->user();
    }

    /**
     * 我的简历
     * @return mixed
     */
    public function myResume(){
        $user = auth()->guard('personal')->user();
        $resumeCount = Resume::where('uid', $user->uid)->where('resume_status', '!=', 4)->count();
        //简历数量
        if($resumeCount > 5){
            return Style::view('personal.resumeNumber');
        }else{
            return Style::view('personal.myResume', compact('user'));
        }
    }

    //我的简历提交
    public function basicInfo(MyResumeRequest $request){
        $user = auth()->guard('personal')->user();
        $resumeCount = Resume::where('uid', $user->uid)->where('resume_status', '!=', 4)->count();
        if($resumeCount > 5){
            return Style::view('personal.resumeNumber');
        }else{
            $input = $request->only(['resume_name', 'name', 'sex', 'birthday', 'job_status', 'education', 'mobile', 'qq', 'email', 'work_year',
                'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5', 'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5',
                'address', 'homeaddress', 'census', 'marital', 'height', 'intentionjobs', 'expectedsalary', 'introduction', 'isexpectedsalary',
                'place_name', 'longitude', 'latitude']);

            $resume = Resume::where('uid', $user->uid)->where('default', 1)->first();
            $input['default'] = (!empty($resume)) ? 0 : 1;
            $input['uid'] = $user->uid;
            $input['language'] = 'cn';
            $input['regtime'] = date('Y-m-d H:i:s', time());
            $input['updatetime'] = date('Y-m-d H:i:s', time());

            //1代表先审核在显示简历
            $input['resume_status'] = (Site::get('resume_review') == '1') ? '0' : '1';
            $input['review'] = 0;//0代表未审核,1代表已处理
            $input['xs_status'] = 0;
            $resume = Resume::create($input);
            return redirect('personal/resume_mange?resume_id=' . $resume->id);
        }
    }

    /**
     * 完善度计算
     * @param $user
     * @param $resume
     * @return int
     */
    public function complete($user, $resume){
        //completion 数组中的 1 代表已经完成 0代表未完成
        $percentage = 0;
        $completionArr = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        //获取百分比
        if(!empty($resume->intentionjobs)) $completionArr[1] = 1;
        if(!empty($resume->introduction)) $completionArr[2] = 1;
        if(ResumeWorkExp::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[3] = 1;
        if(ResumeEducationExp::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[4] = 1;
        if(ResumeProjectExp::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[5] = 1;
        if(ResumeLanguageSkill::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[6] = 1;
        if(ResumeSkill::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[7] = 1;
        if(ResumeCertificate::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[8] = 1;
        if(ResumeOther::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[9] = 1;
        if(ResumeWorks::where('uid', $user->uid)->where('resume_id', $resume->id)->count() > 0) $completionArr[10] = 1;
        //百分比
        for($i = 0; $i < 11; $i++){
            if($i == 8 || $i == 9){
                if($completionArr[$i] == 1){
                    $percentage += 5;
                }
            }else{
                if($completionArr[$i] == 1){
                    $percentage += 10;
                }
            }
        }
        return $percentage . '%';
    }


    /**
     * 简历备份
     * @return mixed
     */
    public function resumeBackup(){
        $user = auth()->guard('personal')->user();

        //默认简历
        $resume = Resume::where('default', 1)->where('uid', $user->uid)->first(['id','language','resume_name', 'intentionjobs', 'introduction', 'updatetime']);
        if(!empty($resume)){
            //完善度
            $resume->percentage = $this->complete($user, $resume);
            //简历投递记录条数
            $resumeSendCount = DB::table('resume_send as t1')
                ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                ->join('company_job as t3', 't1.job_id', '=', 't3.id')
                ->select('t1.id')
                ->where('t1.personal_uid', $user->uid)
                ->where('t1.resume_id', $resume->id)
                ->where('t1.status', '!=', 4)
                ->count();
            $resume->resumeSendCount = ($resumeSendCount > 0) ? $resumeSendCount : 0;
            //谁浏览了我记录条数
            $browseResumeCount = DB::table('browse_resume as t1')
                ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
                ->select('t1.id')
                ->where('t1.personal_uid', $user->uid)
                ->count();
            $resume->browseResumeCount = ($browseResumeCount > 0) ? $browseResumeCount : 0;
            //面试通知记录条数
            $interviewCount = DB::table('interview as t1')
                ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                ->join('company_job as t3', 't1.job_id', '=', 't3.id')
                ->select('t1.id')
                ->where('t1.personal_uid', $user->uid)
                ->where('t1.status', '!=', 4)
                ->count();
            $resume->interviewCount = ($interviewCount > 0) ? $interviewCount : 0;
            $resume->resume_name = (!empty($resume->resume_name)) ? $resume->resume_name : '我的简历';
            $resume->updatetime = substr($resume->updatetime, 0, 10);


            //多份简历
            $moreResume = Resume::where('default', '!=', 1)->where('uid', $user->uid)
                ->select(['id','language', 'resume_name', 'intentionjobs', 'introduction', 'updatetime'])
                ->where('resume_status', '!=', 4)
                ->orderBy('updatetime', 'desc')
                ->take(4)->get();
            if(count($moreResume) > 0){
                foreach($moreResume as $v){
                    //完善度
                    $v->percentage = $this->complete($user, $v);
                    $v->updatetime = substr($v->updatetime, 0, 10);
                    $v->resume_name = (!empty($v->resume_name)) ? $v->resume_name : '我的简历';
                    //简历投递记录条数
                    $resumeSendCount = DB::table('resume_send as t1')
                        ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                        ->join('company_job as t3', 't1.job_id', '=', 't3.id')
                        ->select('t1.id')
                        ->where('t1.personal_uid', $user->uid)
                        ->where('t1.resume_id', $v->id)
                        ->where('t1.status', '!=', 4)
                        ->count();
                    $v->resumeSendCount = ($resumeSendCount > 0) ? $resumeSendCount : 0;
                }
                $countResume = 4 - (int)count($moreResume);
            }else{
                $countResume = 4;
            }
            return Style::view('personal.resumeBackup', compact('resume', 'moreResume', 'countResume'));
        }else{
            return Style::view('personal.warmtips');
        }
    }

    /**
     * 设置默认简历
     * @param $id
     */
    public function setDefault($resume_id){
        if(!empty($resume_id)){
            $user = auth()->guard('personal')->user();
            $result = Resume::where('uid', $user->uid)->where('id', $resume_id)->update(['default' => 1]);
            if($result){
                Resume::where('uid', $user->uid)->where('id', '!=', $resume_id)->update(['default' => 0]);
                return redirect('personal/resume_backup')->with('success', '设置成功');
            }else{
                return back()->with('success', '设置失败,当前简历不存在');
            }
        }
    }

    /**
     * 删除简历
     * @param $id
     */
    public function delResume($resume_id){
        if(!empty($resume_id)){
            $user = auth()->guard('personal')->user();
            $result = Resume::where('uid', $user->uid)->where('id', $resume_id)
                ->update(['resume_status' => '4','review' => '1','xs_status'=>0]);
            if($result){
                return response()->json(['status' => 0, 'msg' => '删除简历成功']);
            }else{
                return response()->json(['status' => 1, 'msg' => '删除简历失败']);
            }
        }
    }


    //-我的简历------------------------------------------------------------------------------------------------------------------
    /**
     * 上传头像
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadAvatar(Request $request){
        if($request->hasFile('file')){
            $file = $request->file('file');
            $entension = strtolower($file->getClientOriginalExtension());
            $clientName = $file->getClientOriginalName();//客服端文件名
            if($file->isValid()){
                if(!in_array($entension, array('gif', 'jpg', 'jpeg', 'bmp', 'png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型只能为jpg、gif、png！"]);
                }else{
                    $resume = Resume::where('uid', $this->user->uid)->where('default', '1')->first(['id', 'avatar']);
                    if(!$resume){
                        return response()->json(['error' => 404, 'info' => "没有找到基本信息！"]);
                    }
                    if($resume->avatar){
                        @unlink(public_path() . '/upload/personalavatar/' . $resume->avatar);
                    }
                    $fileurl = date("Ym") . '/';
                    $filename = time() . strtolower(str_random(5)) . '.' . $entension;
                    $request->file('file')->move(public_path() . '/upload/personalavatar/' . $fileurl, $filename);

                    $resume->avatar = $fileurl . $filename;
                    $resume->xs_status = 0;
                    $resume->save();

                    return response()->json(['status' => 200, 'url' => asset('upload/personalavatar/' . $fileurl . $filename)]);
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件,或者文件过大！"]);
        }
    }

    /**
     * 我的简历管理
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function resumeMange(Request $request){
        //判断是不是默认简历还是当前的简历
        if(!empty($request->resume_id)){
            $resume = Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->first();
        }else{
            $resume = Resume::where('default', 1)->where('language','cn')->where('uid', $this->user->uid)->first();
            if(!isset($resume)){
                $resume = Resume::where('language','cn')->where('uid', $this->user->uid)->orderBy('updatetime','desc')->first();
            }
        }
        if(!empty($resume)){
            //---基本信息--------------------------------------------------------------------------------------------------------------------
            $resumeArr = $resume->toArray();
            $resumeArr['avatar'] = ($resumeArr['avatar']) ? asset('upload/personalavatar/' . $resumeArr['avatar']) : asset('default/images/personal/headPortrait.jpg');
            //基本信息json
            //$currTime = date('Y-m-d');		  //当前时间
            $age = birthday($resumeArr['birthday']);    //年龄
            //工地地区
            $jobarea1 = (strlen($resumeArr['jobarea1']) == 8) ? CollTown::getTownName($resumeArr['jobarea1']) : CollArea::getAreaName($resumeArr['jobarea1']);
            $jobarea2 = (strlen($resumeArr['jobarea2']) == 8) ? CollTown::getTownName($resumeArr['jobarea2']) : CollArea::getAreaName($resumeArr['jobarea2']);
            $jobarea3 = (strlen($resumeArr['jobarea3']) == 8) ? CollTown::getTownName($resumeArr['jobarea3']) : CollArea::getAreaName($resumeArr['jobarea3']);
            $jobarea4 = (strlen($resumeArr['jobarea4']) == 8) ? CollTown::getTownName($resumeArr['jobarea4']) : CollArea::getAreaName($resumeArr['jobarea4']);
            $jobarea5 = (strlen($resumeArr['jobarea5']) == 8) ? CollTown::getTownName($resumeArr['jobarea5']) : CollArea::getAreaName($resumeArr['jobarea5']);
            $jobarea = $jobarea1 . ',' . $jobarea2 . ',' . $jobarea3 . ',' . $jobarea4 . ',' . $jobarea5;
            //工地岗位
            $jobsort1 = ($resumeArr['jobsort1']) ? CollPosition::getPosName($resumeArr['jobsort1']) : '';
            $jobsort2 = ($resumeArr['jobsort2']) ? CollPosition::getPosName($resumeArr['jobsort2']) : '';
            $jobsort3 = ($resumeArr['jobsort3']) ? CollPosition::getPosName($resumeArr['jobsort3']) : '';
            $jobsort4 = ($resumeArr['jobsort4']) ? CollPosition::getPosName($resumeArr['jobsort4']) : '';
            $jobsort5 = ($resumeArr['jobsort5']) ? CollPosition::getPosName($resumeArr['jobsort5']) : '';
            $jobsort = $jobsort1 . ',' . $jobsort2 . ',' . $jobsort3 . ',' . $jobsort4 . ',' . $jobsort5;
            $resumeArr2 = array(
                'job_status' => CollOption::getOptionName($resumeArr['job_status'], 'job_status'),
                'education' => CollOption::getOptionName($resumeArr['education'], 'education'),
                'work_year' => ($resumeArr['work_year']) ? CollOption::getOptionName($resumeArr['work_year'], 'work_year') : "无经验",
                'expectedsalary' => ($resumeArr['expectedsalary']) ? CollOption::getOptionName($resumeArr['expectedsalary'], 'expectedsalary') . '元/月' : '面议',
                'homeaddress' => CollArea::getAreaAddress($resumeArr['homeaddress']),
                'phoneAddress' => CollArea::getAreaName(substr($resumeArr['homeaddress'], 0, 2) . '0000'),
                'jobarea' => msubstr(trim($jobarea, ','), 0, 22),
                'jobsort' => msubstr(trim($jobsort, ','), 0, 22),
                'sex' => ($resumeArr['sex'] == '1') ? '男' : '女',
                'birthday' => $age,
                'census' => ($resumeArr['census']) ? CollArea::getAreaAddress($resumeArr['census']) : '未填写',
                'marital' => ($resumeArr['marital']) ? CollOption::getOptionName($resumeArr['marital'], 'marital') : '未填写',
                'height' => ($resumeArr['height']) ? $resumeArr['height'] . 'cm' : '未填写',
                'avatar' => $resumeArr['avatar']
            );
            $reArr = array_merge($resumeArr, $resumeArr2);
            $resumeJson = json_encode($resumeArr);

            //---求职意向--------------------------------------------------------------------------------------------------------------------
            $jobarea = $jobarea1 . '+' . $jobarea2 . '+' . $jobarea3 . '+' . $jobarea4 . '+' . $jobarea5;
            $jobsort = $jobsort1 . '+' . $jobsort2 . '+' . $jobsort3 . '+' . $jobsort4 . '+' . $jobsort5;
            $objectiveArr = array(
                'intentionjobs' => $resumeArr['intentionjobs'],
                'isexpectedsalary' => $resumeArr['isexpectedsalary'],
                'jobarea1' => $resumeArr['jobarea1'],
                'jobarea2' => $resumeArr['jobarea2'],
                'jobarea3' => $resumeArr['jobarea3'],
                'jobarea4' => $resumeArr['jobarea4'],
                'jobarea5' => $resumeArr['jobarea5'],
                'jobarea' => trim($jobarea, '+'),
                'jobsort1' => $resumeArr['jobsort1'],
                'jobsort2' => $resumeArr['jobsort2'],
                'jobsort3' => $resumeArr['jobsort3'],
                'jobsort4' => $resumeArr['jobsort4'],
                'jobsort5' => $resumeArr['jobsort5'],
                'jobsort' => trim($jobsort, '+'),
                'expectedsalary' => ($resumeArr['expectedsalary']) ? CollOption::getOptionName($resumeArr['expectedsalary'], 'expectedsalary') : '',
            );
            $objectiveJson = json_encode($objectiveArr);

            //---自我评价--------------------------------------------------------------------------------------------------------------------
            $objectiveArr = array(
                'introduction' => $resumeArr['introduction']
            );
            $evaluationJson = json_encode($objectiveArr);

            //---工作经历--------------------------------------------------------------------------------------------------------------------
            $workExp = ResumeWorkExp::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('updatetime', 'desc')->get()->toArray();
            $workExpArr = [];
            if(count($workExp) > 0){
                foreach($workExp as $v){
                    $arr = array(
                        'industry' => ($v['industry']) ? CollOption::getOptionName($v['industry'], 'industry') : "",
                        'comkind' => CollOption::getOptionName($v['comkind'], 'comkind'),
                        'scale' => CollOption::getOptionName($v['scale'], 'employee_num'),
                        'starttime' => substr($v['starttime'], 0, 7),
                        'endtime' => ($v['endtime'] == '0000-00-00') ? '至今' : substr($v['endtime'], 0, 7)
                    );
                    $workExpArr[] = array_merge($v, $arr);
                }
            }
            //---教育经历------------------------------------------------------------------------------------------------------------------------------------------------------
            $eduExp = ResumeEducationExp::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('updatetime', 'desc')->get()->toArray();
            $eduExpArr = [];
            if(count($eduExp) > 0){
                foreach($eduExp as $v){
                    $arr = array(
                        'education' => CollOption::getOptionName($v['education'], 'education'),
                        'starttime' => substr($v['starttime'], 0, 7),
                        'endtime' => ($v['endtime'] == '0000-00-00') ? '至今' : substr($v['endtime'], 0, 7)
                    );
                    $eduExpArr[] = array_merge($v, $arr);
                }
            }
            //---项目经验-------------------------------------------------------------------------------------------------------------------------------------------------------
            $projectExp = ResumeProjectExp::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('updatetime', 'desc')->get()->toArray();
            $projectExpArr = [];
            if(count($projectExp) > 0){
                foreach($projectExp as $v){
                    $arr = array(
                        'starttime' => substr($v['starttime'], 0, 7),
                        'endtime' => ($v['endtime'] == '0000-00-00') ? '至今' : substr($v['endtime'], 0, 7)
                    );
                    $projectExpArr[] = array_merge($v, $arr);
                }
            }
            //---语言能力-------------------------------------------------------------------------------------------------------------------------------------------------------
            $language = ResumeLanguageSkill::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('id')->get()->toArray();
            $languageArr = [];
            if(count($language) > 0){
                foreach($language as $v){
                    $arr = array(
                        'language' => CollOption::getOptionName($v['language'], 'language'),
                        'degree' => CollOption::getOptionName($v['degree'], 'degree'),
                        'level' => CollTwoOption::getTwoOptionName($v['level'], 'language')
                    );
                    $languageArr[] = array_merge($v, $arr);
                }
            }
            $languageJson = json_encode($language);
            //---技能专长-------------------------------------------------------------------------------------------------------------------------------------------------------
            $skill = ResumeSkill::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('id')->get()->toArray();
            $skillArr = [];
            if(count($skill) > 0){
                foreach($skill as $v){
                    $arr = array(
                        'degree' => CollOption::getOptionName($v['degree'], 'degree')
                    );
                    $skillArr[] = array_merge($v, $arr);
                }
            }
            $skillJson = json_encode($skill);
            //---证书-------------------------------------------------------------------------------------------------------------------------------------------------------
            $certificate = ResumeCertificate::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('id')->get()->toArray();
            $certificateArr = [];
            if(count($certificate) > 0){
                foreach($certificate as $v){
                    $arr = array(
                        'gettime' => substr($v['gettime'], 0, 4)
                    );
                    $certificateArr[] = array_merge($v, $arr);
                }
            }
            $certificateJson = json_encode($certificate);
            //dd($certificateJson);
            //---其它信息-------------------------------------------------------------------------------------------------------------------------------------------------------
            $otherArr = ResumeOther::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('updatetime', 'desc')->get()->toArray();
            $otherJson = json_encode($otherArr);

            //---我的作品-------------------------------------------------------------------------------------------------------------------------------------------------------
            $photoJson = [];
            $works = ResumeWorks::where('uid', $this->user->uid)->where('resume_id', $resume->id)->orderBy('updatetime', 'desc')->get()->toArray();
            $worksArr = [];
            $worksType = ['gif', 'jpg', 'jpeg', 'bmp', 'png'];
            foreach($works as $k => $v){
                $worksImgArr = ResumeWorksImg::where('uid', $this->user->uid)->where('works_id', $v['id'])->orderBy('updatetime', 'desc')->get()->toArray();
                if(count($worksImgArr) > 0){
                    foreach($worksImgArr as $kk => $vv){
                        $suffix = substr(strrchr($vv['imgurl'], '.'), 1);
                        if(in_array($suffix, $worksType)){
                            $imgurl = 'upload/personalworks/' . $vv['imgurl'];
                        }else{
                            switch($suffix){
                                case 'doc' :
                                    $imgurl = 'default/images/personal/doc.jpg';
                                    break;
                                case 'docx' :
                                    $imgurl = 'default/images/personal/doc.jpg';
                                    break;
                                case 'ppt' :
                                    $imgurl = 'default/images/personal/ppt.jpg';
                                    break;
                                case 'pptx' :
                                    $imgurl = 'default/images/personal/ppt.jpg';
                                    break;
                                case 'xls' :
                                    $imgurl = 'default/images/personal/xls.jpg';
                                    break;
                                case 'xlsx' :
                                    $imgurl = 'default/images/personal/xls.jpg';
                                    break;
                                case 'pdf' :
                                    $imgurl = 'default/images/personal/pdf.jpg';
                                    break;
                                case 'txt' :
                                    $imgurl = 'default/images/personal/txt.jpg';
                                    break;
                                default :
                                    $imgurl = '未知';
                                    break;
                            }
                        }
                        if($kk == '0'){
                            $v['imgurl'] = asset($imgurl);
                        }
                        $v['allfile'][] = array(
                            'fileUrlImg' => $imgurl,
                            'fileCon' => 'upload/personalworks/' . $vv['imgurl'],
                        );
                        //图片查看
                        $v['photo'][] = array(
                            'alt' => "<a target='_blank' href='" . asset('upload/personalworks/' . $vv['imgurl']) . "'>下载源文件</a>",
                            "pid" => $vv['id'], //图片id
                            "src" => asset($imgurl), //原图地址
                            "thumb" => asset($imgurl)  //缩略图地址
                        );
                        $v['photoJson'] = $v['photo'];
                    }
                }else{
                    $imgurl = 'default/images/personal/null.jpg';
                    $v['imgurl'] = asset($imgurl);
                    $v['allfile'][] = array(
                        'fileUrlImg' => $imgurl,
                        'fileCon' => $imgurl,
                    );
                    //图片查看
                    $v['photo'][] = array(
                        'alt' => "<a target='_blank' href='" . asset($imgurl) . "'>下载源文件</a>",
                        "pid" => $v['id'], //图片id
                        "src" => asset($imgurl), //原图地址
                        "thumb" => asset($imgurl)  //缩略图地址
                    );
                    $v['photoJson'] = $v['photo'];
                }

                $worksArr[] = $v;
                $photoJson[$v['id']] = $v['photoJson'];
            }
            $photoJson = json_encode($photoJson);
            //--------------------------------------------------------------------------------------------------------------------------------------------------
            return Style::view('personal.resumeMange', compact('reArr', 'resumeJson', 'objectiveJson', 'evaluationJson', 'workExpArr', 'eduExpArr', 'projectExpArr'
                , 'languageArr', 'languageJson', 'skillArr', 'skillJson', 'certificateArr', 'certificateJson', 'otherArr', 'otherJson', 'worksArr', 'photoJson'));
        }else{
            return redirect('personal/my_resume');
        }
        //分配简历信息到模板
    }

    /**
     * 更新基本信息
     * @param resumeBasicRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resumeBasic(resumeBasicRequest $request){
        //登录信息
        $input = $request->only(['name', 'sex', 'birthday', 'work_year', 'homeaddress', 'job_status', 'education', 'mobile', 'qq',
            'email', 'chkphoto_open', 'address', 'census', 'marital', 'height','place_name','longitude','latitude']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //1代表先审核在显示简历
        $input['resume_status'] = (Site::get('resume_review') == '1') ? '0' : '1';
        $input['review'] = 0;//0代表未审核,1代表已处理
        $input['xs_status'] = 0;
        Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->update($input);

        //获取更新后的数据
        $resumeArr = Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->first()->toArray();
        $resumeArr['avatar'] = ($resumeArr['avatar']) ? asset('upload/personalavatar/' . $resumeArr['avatar']) : asset('default/images/personal/headPortrait.jpg');

        //$currTime = date('Y-m-d');		  //当前时间
        $age = birthday($resumeArr['birthday']);    //年龄
        //工地地区
        $jobarea1 = (strlen($resumeArr['jobarea1']) == 8) ? CollTown::getTownName($resumeArr['jobarea1']) : CollArea::getAreaName($resumeArr['jobarea1']);
        $jobarea2 = (strlen($resumeArr['jobarea2']) == 8) ? CollTown::getTownName($resumeArr['jobarea2']) : CollArea::getAreaName($resumeArr['jobarea2']);
        $jobarea3 = (strlen($resumeArr['jobarea3']) == 8) ? CollTown::getTownName($resumeArr['jobarea3']) : CollArea::getAreaName($resumeArr['jobarea3']);
        $jobarea4 = (strlen($resumeArr['jobarea4']) == 8) ? CollTown::getTownName($resumeArr['jobarea4']) : CollArea::getAreaName($resumeArr['jobarea4']);
        $jobarea5 = (strlen($resumeArr['jobarea5']) == 8) ? CollTown::getTownName($resumeArr['jobarea5']) : CollArea::getAreaName($resumeArr['jobarea5']);
        $jobarea = $jobarea1 . ',' . $jobarea2 . ',' . $jobarea3 . ',' . $jobarea4 . ',' . $jobarea5;

        $resumeArr2 = array(
            'job_status' => CollOption::getOptionName($resumeArr['job_status'], 'job_status'),
            'education' => CollOption::getOptionName($resumeArr['education'], 'education'),
            'work_year' => ($resumeArr['work_year']) ? CollOption::getOptionName($resumeArr['work_year'], 'work_year') : "无经验",
            'expectedsalary' => ($resumeArr['expectedsalary']) ? CollOption::getOptionName($resumeArr['expectedsalary'], 'expectedsalary') : "面议",
            'homeaddress' => CollArea::getAreaAddress($resumeArr['homeaddress']),
            'phoneAddress' => CollArea::getAreaName(substr($resumeArr['homeaddress'], 0, 2) . '0000'),
            'jobarea' => msubstr(trim($jobarea, ','), 0, 22),
            'sex' => ($resumeArr['sex'] == '1') ? '男' : '女',
            'birthday' => $age,
            'census' => ($resumeArr['census']) ? CollArea::getAreaAddress($resumeArr['census']) : '未填写',
            'marital' => ($resumeArr['marital']) ? CollOption::getOptionName($resumeArr['marital'], 'marital') : '未填写',
            'height' => ($resumeArr['height']) ? $resumeArr['height'] . 'cm' : '未填写'
        );
        $reArr = array_merge($resumeArr, $resumeArr2);
        $arrMerge['one'] = $resumeArr;
        $arrMerge['two'] = $reArr;
        return response()->json($arrMerge);
    }

    /**
     * 更新求职意向
     * @param resumeObjectiveRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resumeObjective(resumeObjectiveRequest $request){
        //登录信息
        $input = $request->only(['intentionjobs', 'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5',
            'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5', 'expectedsalary', 'isexpectedsalary']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //1代表先审核在显示简历
        $input['resume_status'] = (Site::get('resume_review') == '1') ? '0' : '1';
        $input['review'] = 0;//0代表未审核,1代表已处理
        $input['xs_status'] = 0;
        Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->update($input);

        $resumeArr = Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->first()->toArray();
        //工地地区
        $jobarea1 = (strlen($resumeArr['jobarea1']) == 8) ? CollTown::getTownName($resumeArr['jobarea1']) : CollArea::getAreaName($resumeArr['jobarea1']);
        $jobarea2 = (strlen($resumeArr['jobarea2']) == 8) ? CollTown::getTownName($resumeArr['jobarea2']) : CollArea::getAreaName($resumeArr['jobarea2']);
        $jobarea3 = (strlen($resumeArr['jobarea3']) == 8) ? CollTown::getTownName($resumeArr['jobarea3']) : CollArea::getAreaName($resumeArr['jobarea3']);
        $jobarea4 = (strlen($resumeArr['jobarea4']) == 8) ? CollTown::getTownName($resumeArr['jobarea4']) : CollArea::getAreaName($resumeArr['jobarea4']);
        $jobarea5 = (strlen($resumeArr['jobarea5']) == 8) ? CollTown::getTownName($resumeArr['jobarea5']) : CollArea::getAreaName($resumeArr['jobarea5']);
        $jobarea = $jobarea1 . ',' . $jobarea2 . ',' . $jobarea3 . ',' . $jobarea4 . ',' . $jobarea5;
        //工地岗位
        $jobsort1 = ($resumeArr['jobsort1']) ? CollPosition::getPosName($resumeArr['jobsort1']) : '';
        $jobsort2 = ($resumeArr['jobsort2']) ? CollPosition::getPosName($resumeArr['jobsort2']) : '';
        $jobsort3 = ($resumeArr['jobsort3']) ? CollPosition::getPosName($resumeArr['jobsort3']) : '';
        $jobsort4 = ($resumeArr['jobsort4']) ? CollPosition::getPosName($resumeArr['jobsort4']) : '';
        $jobsort5 = ($resumeArr['jobsort5']) ? CollPosition::getPosName($resumeArr['jobsort5']) : '';
        $jobsort = $jobsort1 . ',' . $jobsort2 . ',' . $jobsort3 . ',' . $jobsort4 . ',' . $jobsort5;

        $objectiveArr['arr'] = array(
            'intentionjobs' => $resumeArr['intentionjobs'],
            'isexpectedsalary' => $resumeArr['isexpectedsalary'],
            'jobarea' => msubstr(trim($jobarea, ','), 0, 22),
            'jobsort' => msubstr(trim($jobsort, ','), 0, 22),
            'expectedsalary' => ($resumeArr['expectedsalary']) ? CollOption::getOptionName($resumeArr['expectedsalary'], 'expectedsalary') . '元/月' : '面议',
        );

        $jobarea = $jobarea1 . '+' . $jobarea2 . '+' . $jobarea3 . '+' . $jobarea4 . '+' . $jobarea5;
        $jobsort = $jobsort1 . '+' . $jobsort2 . '+' . $jobsort3 . '+' . $jobsort4 . '+' . $jobsort5;
        $objectiveArr['json'] = array(
            'intentionjobs' => $resumeArr['intentionjobs'],
            'isexpectedsalary' => $resumeArr['isexpectedsalary'],
            'jobarea1' => $resumeArr['jobarea1'],
            'jobarea2' => $resumeArr['jobarea2'],
            'jobarea3' => $resumeArr['jobarea3'],
            'jobarea4' => $resumeArr['jobarea4'],
            'jobarea5' => $resumeArr['jobarea5'],
            'jobarea' => trim($jobarea, '+'),
            'jobsort1' => $resumeArr['jobsort1'],
            'jobsort2' => $resumeArr['jobsort2'],
            'jobsort3' => $resumeArr['jobsort3'],
            'jobsort4' => $resumeArr['jobsort4'],
            'jobsort5' => $resumeArr['jobsort5'],
            'jobsort' => trim($jobsort, '+'),
            'expectedsalary' => ($resumeArr['expectedsalary']) ? CollOption::getOptionName($resumeArr['expectedsalary'], 'expectedsalary') : '',
        );
        return response()->json($objectiveArr);
    }

    /**
     * 更新自我评价
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resumeEvaluation(Request $request){
        $input = $request->only(['introduction']);
        $input['introduction'] = trim($request->introduction);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //1代表先审核在显示简历
        $input['resume_status'] = (Site::get('resume_review') == '1') ? '0' : '1';
        $input['review'] = 0;//0代表未审核,1代表已处理
        $input['xs_status'] = 0;
        Resume::where('id', $request->resume_id)->where('uid', $this->user->uid)->update($input);
        $resumeArr = Resume::where('id', $request->resume_id)->first()->toArray();
        $evaluationArr['json'] = array(
            'introduction' => $resumeArr['introduction']
        );
        return response()->json($evaluationArr);
    }

    /**
     * 检测当前所属用户的简历是不是存在
     * @param $resume_id
     * @return bool
     */
    public function checkResumeExist($resume_id){
        $resume = Resume::where('uid', $this->user->uid)->where('id', $resume_id)->get();
        if(!empty($resume)){
            return true;
        }else{
            return false;
        }
    }

    //工作经历------------------------------------------------------------------------------------------------------------------
    /**
     * 工作经历
     * @param addWorkExpRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addWorkExp(addWorkExpRequest $request){
        $input = $request->only(['company', 'resume_id', 'industry', 'comkind', 'scale', 'starttime', 'endtime', 'post', 'content']);
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //获取用户信息ID
        $input['uid'] = $this->user->uid;
        $input['addtime'] = date('Y-m-d H:i:s', time());
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //返回插入的最后ID
        $id = ResumeWorkExp::insertGetId($input);
        Resume::updateXSStatus($request->resume_id);

        //获取插入后的数据
        $workExpArr = ResumeWorkExp::where('id', $id)->first()->toArray();
        $workExpArr2 = array(
            'industry' => ($workExpArr['industry']) ? CollOption::getOptionName($workExpArr['industry'], 'industry') : "",
            'comkind' => CollOption::getOptionName($workExpArr['comkind'], 'comkind'),
            'scale' => CollOption::getOptionName($workExpArr['scale'], 'employee_num'),
            'starttime' => substr($workExpArr['starttime'], 0, 7),
            'endtime' => ($workExpArr['endtime'] == '0000-00-00') ? '至今' : substr($workExpArr['endtime'], 0, 7)
        );

        $workExp['arr'] = array_merge($workExpArr, $workExpArr2);
        $workExp['json'] = $workExpArr;
        return response()->json($workExp);
    }

    /**
     * 修改工作经历
     * @param modWorkExpRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function modWorkExp(modWorkExpRequest $request){
        $input = $request->only(['company', 'industry', 'comkind', 'scale', 'starttime', 'endtime', 'post', 'content']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        ResumeWorkExp::where('id', $request->id)->where('uid', $this->user->uid)->update($input);

        //获取修改后的数据
        $workExpArr = ResumeWorkExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        Resume::updateXSStatus($workExpArr['resume_id']);
        $workExpArr2 = array(
            'industry' => ($workExpArr['industry']) ? CollOption::getOptionName($workExpArr['industry'], 'industry') : "",
            'comkind' => CollOption::getOptionName($workExpArr['comkind'], 'comkind'),
            'scale' => CollOption::getOptionName($workExpArr['scale'], 'employee_num'),
            'starttime' => substr($workExpArr['starttime'], 0, 7),
            'endtime' => ($workExpArr['endtime'] == '0000-00-00') ? '至今' : substr($workExpArr['endtime'], 0, 7)
        );

        $workExp['arr'] = array_merge($workExpArr, $workExpArr2);
        $workExp['json'] = $workExpArr;
        return response()->json($workExp);
    }

    /**
     *编辑工作经历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editWorkExp(Request $request){
        $workExpArr = ResumeWorkExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        return response()->json($workExpArr);
    }

    /**
     * 删除工作经历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delWorkExp(Request $request){
        $result = ResumeWorkExp::where('id', $request->id)
            ->where('uid', $this->user->uid)
            ->first(['id','resume_id']);
        if($result->delete()){
            Resume::updateXSStatus($result->resume_id);
            return response()->json(['status' => 0, 'msg' => '删除工作经历成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除工作经历失败，请稍后重试！']);
        }
    }

    //教育经历------------------------------------------------------------------------------------------------------------------
    /**
     * 教育经历
     * @param addEducationExpRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addEduExp(addEducationExpRequest $request){
        if($request->edu_type == '1')
            $input = $request->only(['resume_id', 'school', 'starttime', 'endtime', 'speciality', 'education', 'type', 'edu_type', 'description']);
        else
            $input = $request->only(['resume_id', 'school', 'starttime', 'endtime', 'speciality', 'type', 'edu_type', 'description']);

        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //获取用户信息ID
        $input['uid'] = $this->user->uid;
        $input['type'] = empty($request->type) ? '0' : $request->type;
        $input['addtime'] = date('Y-m-d H:i:s', time());
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //返回插入的最后ID
        $id = ResumeEducationExp::insertGetId($input);
        Resume::updateXSStatus($request->resume_id);

        //获取插入后的数据
        $eduExpArr = ResumeEducationExp::where('id', $id)->first()->toArray();
        $eduExpArr2 = array(
            'education' => CollOption::getOptionName($eduExpArr['education'], 'education'),
            'starttime' => substr($eduExpArr['starttime'], 0, 7),
            'endtime' => ($eduExpArr['endtime'] == '0000-00-00') ? '至今' : substr($eduExpArr['endtime'], 0, 7)
        );

        $eduExp['arr'] = array_merge($eduExpArr, $eduExpArr2);
        $eduExp['json'] = $eduExpArr;
        return response()->json($eduExp);
    }

    /**
     * 修改教育经历
     * @param modEducationExpRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function modEduExp(modEducationExpRequest $request){
        if($request->edu_type == '1')
            $input = $request->only(['resume_id', 'school', 'starttime', 'endtime', 'speciality', 'education', 'type', 'edu_type', 'description']);
        else
            $input = $request->only(['resume_id', 'school', 'starttime', 'endtime', 'speciality', 'type', 'edu_type', 'description']);

        $input['type'] = empty($request->type) ? '0' : $request->type;
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        ResumeEducationExp::where('id', $request->id)->where('uid', $this->user->uid)->update($input);

        //获取插入后的数据
        $eduExpArr = ResumeEducationExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        Resume::updateXSStatus($eduExpArr['resume_id']);
        $eduExpArr2 = array(
            'education' => CollOption::getOptionName($eduExpArr['education'], 'education'),
            'starttime' => substr($eduExpArr['starttime'], 0, 7),
            'endtime' => ($eduExpArr['endtime'] == '0000-00-00') ? '至今' : substr($eduExpArr['endtime'], 0, 7)
        );

        $eduExp['arr'] = array_merge($eduExpArr, $eduExpArr2);
        $eduExp['json'] = $eduExpArr;
        return response()->json($eduExp);
    }

    /**
     * 编辑教育经历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editEduExp(Request $request){
        $eduExpArr = ResumeEducationExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        return response()->json($eduExpArr);
    }

    /**
     * 编辑教育经历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delEduExp(Request $request){
        $result = ResumeEducationExp::where('id', $request->id)
            ->where('uid', $this->user->uid)
            ->first(['id','resume_id']);
        if($result->delete()){
            Resume::updateXSStatus($result->resume_id);
            return response()->json(['status' => 0, 'msg' => '删除教育经历成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除教育经历失败，请稍后重试！']);
        }
    }

    //项目经验------------------------------------------------------------------------------------------------------------------
    /**
     * 项目经验
     * @param addProjectExpRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addProjectExp(addProjectExpRequest $request){
        $input = $request->only(['resume_id', 'project_name', 'starttime', 'endtime', 'post', 'content']);
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //获取用户信息ID
        $input['uid'] = $this->user->uid;
        $input['addtime'] = date('Y-m-d H:i:s', time());
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //返回插入的最后ID
        $id = ResumeProjectExp::insertGetId($input);
        Resume::updateXSStatus($request->resume_id);

        //获取插入后的数据
        $projectExpArr = ResumeProjectExp::where('id', $id)->first()->toArray();
        $projectExpArr2 = array(
            'starttime' => substr($projectExpArr['starttime'], 0, 7),
            'endtime' => ($projectExpArr['endtime'] == '0000-00-00') ? '至今' : substr($projectExpArr['endtime'], 0, 7)
        );

        $projectExp['arr'] = array_merge($projectExpArr, $projectExpArr2);
        $projectExp['json'] = $projectExpArr;
        return response()->json($projectExp);
    }

    /**
     * 修改项目经验
     * @param modProjectExpRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function modProjectExp(modProjectExpRequest $request){
        $input = $request->only(['resume_id', 'project_name', 'starttime', 'endtime', 'post', 'content']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        ResumeProjectExp::where('id', $request->id)->where('uid', $this->user->uid)->update($input);

        //获取插入后的数据
        $projectExpArr = ResumeProjectExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        Resume::updateXSStatus($projectExpArr['resume_id']);
        $projectExpArr2 = array(
            'starttime' => substr($projectExpArr['starttime'], 0, 7),
            'endtime' => ($projectExpArr['endtime'] == '0000-00-00') ? '至今' : substr($projectExpArr['endtime'], 0, 7)
        );

        $projectExp['arr'] = array_merge($projectExpArr, $projectExpArr2);
        $projectExp['json'] = $projectExpArr;
        return response()->json($projectExp);
    }

    /**
     * 编辑项目经验
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editProjectExp(Request $request){
        $projectExpArr = ResumeProjectExp::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        return response()->json($projectExpArr);
    }

    /**
     * 删除项目经验
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delProjectExp(Request $request){
        $result = ResumeProjectExp::where('id', $request->id)
            ->where('uid', $this->user->uid)
            ->first(['id','resume_id']);
        if($result->delete()){
            Resume::updateXSStatus($result->resume_id);
            return response()->json(['status' => 0, 'msg' => '删除项目经验成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除项目经验失败，请稍后重试！']);
        }
    }

    //语言能力------------------------------------------------------------------------------------------------------------------
    /**
     * 语言能力
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addLanguage(Request $request){
        ///检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //编辑不存在数组时删除数据
        $languageid = array_column(ResumeLanguageSkill::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->get()->toArray(), 'id');
        if(count($languageid) > 0){
            $id = $request->input('id.*');
            if(count($id) > 0){
                $result = array_diff($languageid, $id);
            }else{
                $result = $languageid;
            }
            if(count($result) > 0){
                foreach($result as $k => $v){
                    ResumeLanguageSkill::where('id', $v)->where('uid', $this->user->uid)->delete();
                }
            }
        }

        //编辑存在数组时更新数据
        $input = $request->only(['resume_id', 'language', 'degree', 'level']);
        if(count($request->input('language.*')) > 0){
            $language = $request->input('language.*');
            $degree = $request->input('degree.*');
            $level = $request->input('level.*');
            $id = $request->input('id.*');
            foreach($language as $k => $v){
                if(!empty($v)){
                    $input['language'] = $v;
                    $input['degree'] = $degree[$k];
                    $input['level'] = $level[$k];
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeLanguageSkill::where('id', $id[$k])->where('uid', $this->user->uid)->update($input);
                }
            }
        };
        //不存在数组时添加数据
        if(count($request->input('addLanguage.*')) > 0){
            $addLanguage = $request->input('addLanguage.*');
            $addDegree = $request->input('addDegree.*');
            $addLevel = $request->input('addLevel.*');
            $input['uid'] = $this->user->uid;
            $input['resume_id'] = $request->input('resume_id');
            foreach($addLanguage as $k => $v){
                if(!empty($v)){
                    $input['language'] = $v;
                    $input['degree'] = $addDegree[$k];
                    $input['level'] = $addLevel[$k];
                    $input['addtime'] = date('Y-m-d H:i:s', time());
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeLanguageSkill::create($input);
                }
            }
        }

        //获取插入后的数据
        $language = ResumeLanguageSkill::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->orderBy('id')->get()->toArray();
        if(count($language) > 0){
            $languageArr = [];
            foreach($language as $v){
                $arr = array(
                    'language' => CollOption::getOptionName($v['language'], 'language'),
                    'degree' => CollOption::getOptionName($v['degree'], 'degree'),
                    'level' => CollTwoOption::getTwoOptionName($v['level'], 'language')
                );
                $languageArr[] = array_merge($v, $arr);
            }
            $languageJson['arr'] = $languageArr;
            $languageJson['json'] = $language;
            return response()->json($languageJson);
        }else{
            $languageJson['arr'] = '';
            $languageJson['json'] = '';
            return response()->json($languageJson);
        }

    }

    //技能专长------------------------------------------------------------------------------------------------------------------
    /**
     * 技能专长
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addSkill(Request $request){
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }

        $delete_bl = false;
        $update_bl = false;
        $create_bl = false;

        //编辑不存在数组时删除数据
        $skillid = array_column(ResumeSkill::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->get()->toArray(), 'id');
        if(count($skillid) > 0){
            $id = $request->input('id.*');
            if(count($id) > 0){
                $result = array_diff($skillid, $id);
            }else{
                $result = $skillid;
            }
            if(count($result) > 0){
                foreach($result as $k => $v){
                    ResumeSkill::where('id', $v)->where('uid', $this->user->uid)->delete();
                    $delete_bl = true;
                }
            }
        }
        //编辑存在数组时更新数据
        $input = $request->only(['resume_id', 'skillname', 'degree']);
        if(count($request->input('skillname.*')) > 0){
            $skillname = $request->input('skillname.*');
            $degree = $request->input('degree.*');
            $id = $request->input('id.*');
            foreach($skillname as $k => $v){
                if(!empty($v)){
                    $input['skillname'] = $v;
                    $input['degree'] = $degree[$k];
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeSkill::where('id', $id[$k])->where('uid', $this->user->uid)->update($input);
                    $update_bl = true;
                }
            }
        };
        //不存在数组时添加数据
        if(count($request->input('addskillname.*')) > 0){
            $addskillname = $request->input('addskillname.*');
            $addDegree = $request->input('addDegree.*');
            $input['uid'] = $this->user->uid;
            $input['resume_id'] = $request->input('resume_id');
            foreach($addskillname as $k => $v){
                if(!empty($v)){
                    $input['skillname'] = $addskillname[$k];
                    $input['degree'] = $addDegree[$k];
                    $input['addtime'] = date('Y-m-d H:i:s', time());
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeSkill::create($input);
                    $create_bl = true;
                }
            }
        }

        if ($delete_bl || $update_bl || $create_bl) {
            Resume::updateXSStatus($request->resume_id);
        }

        //获取插入后的数据
        $skill = ResumeSkill::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->orderBy('id')->get()->toArray();
        if(count($skill) > 0){
            $skillArr = [];
            foreach($skill as $v){
                $arr = array(
                    'degree' => CollOption::getOptionName($v['degree'], 'degree')
                );
                $skillArr[] = array_merge($v, $arr);
            }
            $skillJson['arr'] = $skillArr;
            $skillJson['json'] = $skill;
            return response()->json($skillJson);
        }else{
            $skillJson['arr'] = '';
            $skillJson['json'] = '';
            return response()->json($skillJson);
        }
    }

    //证书---------------------------------------------------------------------------------------------------------------------
    /**
     * 证书
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addCertificate(Request $request){
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //编辑不存在数组时删除数据
        $certificateid = array_column(ResumeCertificate::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->get()->toArray(), 'id');
        if(count($certificateid) > 0){
            $id = $request->input('id.*');
            if(count($id) > 0){
                $result = array_diff($certificateid, $id);
            }else{
                $result = $certificateid;
            }
            if(count($result) > 0){
                foreach($result as $k => $v){
                    ResumeCertificate::where('id', $v)->where('uid', $this->user->uid)->delete();
                }
            }
        }
        //编辑存在数组时更新数据
        $input = $request->only(['resume_id', 'certificate_name', 'gettime']);
        if(count($request->input('certificate_name.*')) > 0){
            $certificate_name = $request->input('certificate_name.*');
            $gettime = $request->input('gettime.*');
            $id = $request->input('id.*');
            foreach($certificate_name as $k => $v){
                if(!empty($v)){
                    $input['certificate_name'] = $v;
                    $input['gettime'] = $gettime[$k];
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeCertificate::where('id', $id[$k])->where('uid', $this->user->uid)->update($input);
                }
            }
        };
        //不存在数组时添加数据
        if(count($request->input('addcertificate_name.*')) > 0){
            $addcertificate_name = $request->input('addcertificate_name.*');
            $addGettime = $request->input('addGettime.*');
            $input['uid'] = $this->user->uid;
            $input['resume_id'] = $request->input('resume_id');
            foreach($addGettime as $k => $v){
                if(!empty($v)){
                    $input['certificate_name'] = $addcertificate_name[$k];
                    $input['gettime'] = $addGettime[$k];
                    $input['addtime'] = date('Y-m-d H:i:s', time());
                    $input['updatetime'] = date('Y-m-d H:i:s', time());
                    ResumeCertificate::create($input);
                }
            }
        }
        //获取插入后的数据
        $certificate = ResumeCertificate::where('uid', $this->user->uid)->where('resume_id', $request->input('resume_id'))->orderBy('id')->get()->toArray();
        if(count($certificate) > 0){
            $certificateArr = [];
            foreach($certificate as $v){
                $arr = array(
                    'gettime' => substr($v['gettime'], 0, 4)
                );
                $certificateArr[] = array_merge($v, $arr);
            }
            $certificateJson['arr'] = $certificateArr;
            $certificateJson['json'] = $certificate;
            return response()->json($certificateJson);
        }else{
            $certificateJson['arr'] = '';
            $certificateJson['json'] = '';
            return response()->json($certificateJson);
        }
    }

    //其它信息------------------------------------------------------------------------------------------------------------------
    /**
     * 其它信息
     * @param addOtherRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addOther(addOtherRequest $request){
        $input = $request->only(['resume_id', 'title', 'content']);
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //获取用户信息ID
        $input['uid'] = $this->user->uid;
        $input['addtime'] = date('Y-m-d H:i:s', time());
        $input['updatetime'] = date('Y-m-d H:i:s', time());

        if($request->title == 'zdy'){
            $input['title'] = $request->titInp;
        }else{
            $CollOption = CollOption::where('opt_id', $request->title)->where('sign', 'title')->first()->toArray();
            $input['title'] = $CollOption['opt_name'];
        }
        //返回插入的最后ID
        $id = ResumeOther::insertGetId($input);

        //获取插入后的数据
        $otherArr = ResumeOther::where('id', $id)->first()->toArray();
        $other['arr'] = $otherArr;
        $other['json'] = $otherArr;
        $other['newJson'] = ResumeOther::where('uid', $this->user->uid)->where('resume_id', $request->resume_id)->get()->toArray();
        return response()->json($other);
    }

    /**
     * 修改其它信息
     * @param modOtherRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function modOther(modOtherRequest $request){
        $input = $request->only(['resume_id', 'title', 'content']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());

        if($request->title == 'zdy'){
            $input['title'] = $request->titInp;
        }else{
            $CollOption = CollOption::where('opt_id', $request->title)->where('sign', 'title')->first()->toArray();
            $input['title'] = $CollOption['opt_name'];
        }
        ResumeOther::where('id', $request->id)->update($input);

        //获取插入后的数据
        $otherArr = ResumeOther::where('id', $request->id)->first()->toArray();
        $other['arr'] = $otherArr;
        $other['json'] = $otherArr;
        $other['newJson'] = ResumeOther::where('uid', $this->user->uid)->where('resume_id', $request->resume_id)->get()->toArray();
        return response()->json($other);
    }

    /**
     * 编辑其它信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editOther(Request $request){
        $otherArr = ResumeOther::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        return response()->json($otherArr);
    }

    /**
     * 删除其它信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delOther(Request $request){
        $result = ResumeOther::where('id', $request->id)->where('uid', $this->user->uid)->delete();
        if($result){
            return response()->json(['status' => 0, 'msg' => '删除其它信息成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除其它信息失败，请稍后重试！']);
        }
    }

    //我的作品------------------------------------------------------------------------------------------------------------------
    /**
     * 我的作品
     * @param addWorksRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function addWorks(addWorksRequest $request){
        $input = $request->only(['resume_id', 'works_name', 'description']);
        //检测当前所属用户的简历是不是存在
        if(!$this->checkResumeExist($request->resume_id)){
            return response('当前所属用户的简历不存在', 501);
        }
        //获取用户信息ID
        $input['uid'] = $this->user->uid;
        $input['addtime'] = date('Y-m-d H:i:s', time());
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        //返回插入的最后ID
        $id = ResumeWorks::insertGetId($input);
        //获取插入后的数据
        $worksArr = ResumeWorks::where('id', $id)->first()->toArray();
        return response()->json($worksArr);
    }

    /**
     * 修改我的作品
     * @param modWorksRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function modWorks(modWorksRequest $request){
        $input = $request->only(['resume_id', 'works_name', 'description']);
        $input['updatetime'] = date('Y-m-d H:i:s', time());
        ResumeWorks::where('id', $request->input('id'))->where('uid', $this->user->uid)->update($input);

        //判断修改的文件要不要更新
        $worksimgid = array_column(ResumeWorksImg::where('uid', $this->user->uid)->where('works_id', $request->input('id'))->get()->toArray(), 'id');
        if(count($worksimgid) > 0){
            $w_id = $request->input('w_id.*');
            if(count($w_id) > 0){
                $result = array_diff($worksimgid, $w_id);
            }else{
                $result = $worksimgid;
            }
            foreach($result as $k => $v){
                $ResumeWorksImg = ResumeWorksImg::where('id', $v)->where('uid', $this->user->uid)->first(['imgurl'])->toArray();
                @unlink(public_path() . '/upload/personalworks/' . $ResumeWorksImg['imgurl']);
                ResumeWorksImg::where('id', $v)->where('uid', $this->user->uid)->delete();
            }
        }
        //获取插入后的数据
        $worksArr = ResumeWorks::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        //插入的ID
        $worksArr['w_id'] = $request->input('w_id.*');
        $worksImgArr = ResumeWorksImg::where('works_id', $request->id)->where('uid', $this->user->uid)->orderBy('updatetime', 'desc')->get()->toArray();
        if(count($worksImgArr) > 0){
            $worksType = ['gif', 'jpg', 'jpeg', 'bmp', 'png'];
            foreach($worksImgArr as $kk => $vv){
                $suffix = substr(strrchr($vv['imgurl'], '.'), 1);
                if(in_array($suffix, $worksType)){
                    $imgurl = 'upload/personalworks/' . $vv['imgurl'];
                }else{
                    switch($suffix){
                        case 'doc' :
                            $imgurl = 'default/images/personal/doc.jpg';
                            break;
                        case 'docx' :
                            $imgurl = 'default/images/personal/doc.jpg';
                            break;
                        case 'ppt' :
                            $imgurl = 'default/images/personal/ppt.jpg';
                            break;
                        case 'pptx' :
                            $imgurl = 'default/images/personal/ppt.jpg';
                            break;
                        case 'xls' :
                            $imgurl = 'default/images/personal/xls.jpg';
                            break;
                        case 'xlsx' :
                            $imgurl = 'default/images/personal/xls.jpg';
                            break;
                        case 'pdf' :
                            $imgurl = 'default/images/personal/pdf.jpg';
                            break;
                        case 'txt' :
                            $imgurl = 'default/images/personal/txt.jpg';
                            break;
                        default :
                            $imgurl = '未知';
                            break;
                    }
                }
                if($kk == '0'){
                    $worksArr['imgurl'] = asset($imgurl);
                }
                $worksArr['allfile'][] = array(
                    'fileUrlImg' => asset($imgurl),
                    'fileCon' => asset('upload/personalworks/' . $vv['imgurl'])
                );

                //图片查看
                $worksArr['photo'][] = array(
                    'alt' => "<a target='_blank' href='" . asset('upload/personalworks/' . $vv['imgurl']) . "'>下载源文件</a>",
                    "pid" => $vv['id'], //图片id
                    "src" => asset($imgurl), //原图地址
                    "thumb" => asset($imgurl)  //缩略图地址
                );
                $worksArr['photoJson'] = json_encode($worksArr['photo']);
            }
        }else{
            $worksArr['allfile'] = '';
            $worksArr['photoJson'] = '';
        }
        // dd($worksArr);
        return response()->json($worksArr);
    }

    /**
     * 编辑我的作品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editWorks(Request $request){
        $worksArr = ResumeWorks::where('id', $request->id)->where('uid', $this->user->uid)->first()->toArray();
        $worksImgArr = ResumeWorksImg::where('uid', $this->user->uid)->where('works_id', $request->id)->orderBy('updatetime', 'desc')->get()->toArray();
        if(count($worksImgArr) > 0){
            foreach($worksImgArr as $kk => $vv){
                $worksArr['allfile'][] = array(
                    'originalimg' => $vv['originalimg'],
                    'id' => $vv['id']
                );
            }
        }
        return response()->json($worksArr);
    }

    /**
     * 删除我的作品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delWorks(Request $request){
        $result = ResumeWorks::where('id', $request->id)->where('uid', $this->user->uid)->delete();
        if($result){
            //删除图片
            $worksimgid = array_column(ResumeWorksImg::where('uid', $this->user->uid)->where('works_id', $request->input('id'))->get()->toArray(), 'id');
            foreach($worksimgid as $k => $v){
                $ResumeWorksImg = ResumeWorksImg::where('id', $v)->first(['imgurl'])->toArray();
                @unlink(public_path() . '/upload/personalworks/' . $ResumeWorksImg['imgurl']);
                ResumeWorksImg::where('id', $v)->delete();
            }
            return response()->json(['status' => 0, 'msg' => '删除我的作品成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除我的作品失败，请稍后重试！']);
        }
    }

    //刷新简历------------------------------------------------------------------------------------------------------------------
    /**
     * 刷新简历
     * @param Request $request
     * @return mixed
     */
    public function resumeRefresh(Request $request){

        $resume = Resume::where('uid', $this->user->uid)->where(function($query) use ($request){
            if(!empty($request->resume_id)){
                $query->where('id', $request->resume_id);
            }else{
                $query->where('default', 1);
            }
        })->first(['id', 'work_year', 'mobile', 'email', 'qq', 'address','job_status', 'updatetime']);
        if(!empty($resume)){
            return Style::view('personal.resumeRefresh', compact('resume'));
        }else{
            return Style::view('personal.warmtips');
        }

    }

    /**
     * 刷新简历提交
     * @param resumeRefreshRequest $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postResumeRefresh(resumeRefreshRequest $request){
        $input = $request->only(['id', 'work_year', 'mobile', 'email', 'qq','job_status', 'address']);
        if($request->uptype == 1){
            $input['updatetime'] = date('Y-m-d H:i:s', time());
            $input['xs_status']=0;
        }
        $result = Resume::where('id', $request->id)->where('uid', $this->user->uid)->update($input);
        if($request->requestajax == 1){
            if($result){
                return response()->json(['status' => 0, 'msg' => '刷新简历成功！']);
            }else{
                return response()->json(['status' => 1, 'msg' => '刷新简历失败，可能更新前和更新后的数据一致！']);
            }
        }else{
            return redirect('personal/welcome');
        }
    }

    /**
     * 个人简历地标地图设置
     * @param resumeRefreshRequest $request
     */
    public function placeMap(Request $request){
        $resume = Resume::where('id', $request->resume_id)->first(['place_name', 'longitude', 'latitude']);
        return Style::view("personal.placeMap", compact('resume'));
    }

}